﻿using System.Drawing;
using System.Text;

namespace Kaleida.ServiceMonitor.Model.Runtime
{
    public class DefaultStatePresentation : IStatePresentation
    {
        private readonly MonitorState state;

        public DefaultStatePresentation(MonitorState state)
        {
            this.state = state;
        }

        public Color GetNotificationColour()
        {
            return state.ToSeverityLevel().ToColour();
        }

        public string GetNotificationWord()
        {
            if (state.HasScriptError)
                return "!";

            if (state.HasOperationFailures)
                return ":-(";

            if (state.IsMonitoring)
                return ":-)";
            
            return "";
        }

        public string GetStateSummary()
        {
            if (state.HasOperationFailures)
                return string.Format("{0} error(s)", state.OperationErrorCount);

            if (state.HasScriptError)
                return "Script Error";

            if (state.IsMonitoring)
                return "Monitoring";
            
            return "Not Monitoring";
        }

        public string GetStateDetail()
        {
            if (!state.HasOperationFailures)
                return "";
        
            var description = new StringBuilder();

            description.Append(state.OperationErrorCount);
            description.AppendFormat(" error{0}:\r\n", state.OperationErrorCount == 1 ? "" : "s");

            if (state.FirstOperationError != state.LatestOperationError)
            {
                description.AppendFormat("    First:  {0}\r\n", state.FirstOperationError);
                description.AppendFormat("    Latest: {0}\r\n", state.LatestOperationError);
            }
            else
            {
                description.AppendFormat("    {0}\r\n", state.LatestOperationError);
            }

            return description.ToString();
        }
    }
}
